package com.sikao.app.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.bean.BuyerOrderOperateSubmitBean;
import com.sikao.app.bean.OrderEvaluationBean;
import com.sikao.app.bean.OrderSubmitId;
import com.sikao.app.dao.GetBuyerEvaluationModel;
import com.sikao.app.dao.GetSellerEvaluationModel;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;

import static com.baidu.location.h.i.o;

/**
 * 查看评价
 */
public class EvaluationActivity extends BaseActivity implements View.OnClickListener {

	private TextView titleTv;
	private ImageView backIv;
	private TextView tvType1;
	private TextView tvEvaluation;
	private TextView tvType2;
	private RatingBar ratingBar1;
	private RatingBar ratingBar2;
	private RatingBar ratingBar3;

	private GetSellerEvaluationModel getSellerEvaluationModel;
	private GetBuyerEvaluationModel getBuyerEvaluationModel;
	private BuyerOrderOperateSubmitBean orderOperateSubmitBean;
	private String beanId;
	private String type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_evaluation);
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initData() {
		beanId = getIntent().getStringExtra("bean");
		type = getIntent().getStringExtra("type");
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		backIv = (ImageView) findViewById(R.id.title_left_img);
		tvType1 = (TextView) findViewById(R.id.tv_type1);
		tvEvaluation = (TextView) findViewById(R.id.tv_evaluation);
		tvType2 = (TextView) findViewById(R.id.tv_type2);
		ratingBar1 = (RatingBar) findViewById(R.id.ratingBar1);
		ratingBar2 = (RatingBar) findViewById(R.id.ratingBar2);
		ratingBar3 = (RatingBar) findViewById(R.id.ratingBar3);
	}

	@Override
	protected void initListener() {
		backIv.setOnClickListener(this);
	}


	@Override
	protected void init() {
		titleTv.setText("评价详情");
		backIv.setVisibility(View.VISIBLE);
		if (type.equals("buyer")){
			tvType1.setText("我的评价：");
			tvType2.setText("我的评分");
		}
		getEvaluation();
	}

	private void getEvaluation() {
		if (type.equals("buyer")){
			if (getBuyerEvaluationModel == null) {
				orderOperateSubmitBean = new BuyerOrderOperateSubmitBean();
				orderOperateSubmitBean.setOrderId(beanId);
				orderOperateSubmitBean.setAction("getComment");
				getBuyerEvaluationModel = new GetBuyerEvaluationModel(this, new OnNetBackListener<OrderEvaluationBean>() {
					@Override
					public void netBack(OrderEvaluationBean data) {
						if (data == null) {
							return;
						}
						if (data.getStatus().equals("1")){
							showContent(data);
						}else{
							Toast.makeText(EvaluationActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
			getBuyerEvaluationModel.request(JSONUtil.toJSON(orderOperateSubmitBean), true);
		}else{
			if (getSellerEvaluationModel == null) {
				orderOperateSubmitBean = new BuyerOrderOperateSubmitBean();
				orderOperateSubmitBean.setOrderId(beanId);
				orderOperateSubmitBean.setAction("getComment");
				getSellerEvaluationModel = new GetSellerEvaluationModel(this, new OnNetBackListener<OrderEvaluationBean>() {
					@Override
					public void netBack(OrderEvaluationBean data) {
						if (data == null) {
							return;
						}
						if (data.getStatus().equals("1")){
							showContent(data);
						}else{
							Toast.makeText(EvaluationActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				});
			}
			getSellerEvaluationModel.request(JSONUtil.toJSON(orderOperateSubmitBean), true);
		}

	}

	private void showContent(OrderEvaluationBean bean){
		tvEvaluation.setText(bean.getRes().get(0).getContent());
		ratingBar1.setRating(Float.parseFloat(bean.getRes().get(0).getScore_1()));
		ratingBar2.setRating(Float.parseFloat(bean.getRes().get(0).getScore_2()));
		ratingBar3.setRating(Float.parseFloat(bean.getRes().get(0).getScore_3()));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				finish();
				break;
			default:
				break;
		}
	}
}
